/**
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 */
package com.jeesite.modules.storage.api;

import com.jeesite.common.lang.StringUtils;
import com.jeesite.modules.storage.service.AddressService;
import com.jeesite.modules.storage.common.ServerResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 收货地址Controller
 */
@RestController(value = "apiaddress")
@RequestMapping(value = "/api/address")
public class AddressController extends APIController {

    @Autowired
    private AddressService addressService;

    /**
     * 查询会员收货地址列表
     * @param transMemberId 转发会员Id
     * @param memberId 会员Id
     * @param size 每页数量
     * @param page 页码
     * @return 会员收货地址列表
     */
    @RequestMapping(method = RequestMethod.GET,value = "query")
    @ResponseBody
    public ServerResponse<List<Map>> query(@RequestParam(value="transMemberId",required = false) String transMemberId,@RequestParam(value="memberId") String memberId, @RequestParam(value="size",defaultValue = "5") Integer size, @RequestParam(value="page",defaultValue = "1") Integer page)
    {
        return  ServerResponse.createBySuccess(200,addressService.getListByMemberId(transMemberId,memberId,size,page));
    }

    /**
     * 保存
     * @param transMemberId 转发会员Id
     * @param memberId 会员Id
     * @param addressId 收货地址Id
     * @param name 姓名
     * @param phone 手机号
     * @param province 省份
     * @param city 城市
     * @param county 县镇
     * @param address 详细地址
     * @param isDefault 是否默认
     * @return
     */
    @RequestMapping(method = RequestMethod.POST,value = "save")
    public ServerResponse<String> save(@RequestParam(value="transMemberId") String transMemberId,@RequestParam(value="memberId") String memberId,@RequestParam(value="addressId") String addressId,@RequestParam(value="name") String name,@RequestParam(value="phone") String phone,@RequestParam(value="province") String province,@RequestParam(value="city") String city,@RequestParam(value="county") String county,@RequestParam(value="address") String address,@RequestParam(value="isDefault") Boolean isDefault)
    {
        Integer code = addressService.save(transMemberId,memberId,addressId,name,phone,province,city,county,address,isDefault);
        String msg = "";
        switch (code){
            case 200:
                msg = StringUtils.isNotBlank(addressId)?"新增成功": "保存成功";
                break;
            case 201:
                msg = "不符合修改【每月仅修改一次】";
                break;
            default:
                msg = "发生其它异常";
                break;
        }
        return  ServerResponse.createBySuccess(code,msg);
    }

    /**
     * 设置默认
     * @param transMemberId 转发会员Id
     * @param memberId 会员Id
     * @param addressId 收货地址Id
     * @return
     */
    @RequestMapping(method = RequestMethod.POST,value = "set/default")
    public ServerResponse<String> setIsDefault(@RequestParam(value="transMemberId",required = false) String transMemberId,@RequestParam(value="memberId") String memberId, @RequestParam(value="addressId") String addressId)
    {
        Integer code = addressService.setIsDefault(transMemberId,memberId,addressId);
        String msg = "";
        switch (code){
            case 200:
                msg = "更新成功";
                break;
            case 201:
                msg = "不符合修改【每月仅修改一次】";
                break;
            default:
                msg = "发生其它异常";
                break;
        }
        return  ServerResponse.createBySuccess(code,msg);
    }

    /**
     * 删除
     * @param addressId 收货地址Id
     * @return
     */
    @RequestMapping(method = RequestMethod.POST,value = "remove")
    public ServerResponse<String> remove(@RequestParam(value="addressId") String addressId)
    {
        Integer code = addressService.remove(addressId);
        String msg = "";
        switch (code){
            case 200:
                msg = "删除成功";
                break;
            default:
                msg = "发生其它异常";
                break;
        }
        return  ServerResponse.createBySuccess(code,msg);
    }

    /**
     * 获取收货地址
     * @param addressId 收货地址Id
     * @return
     */
    @RequestMapping(method = RequestMethod.GET,value = "content")
    @ResponseBody
    public ServerResponse<Map> content(@RequestParam(value="addressId") String addressId)
    {
        return  ServerResponse.createBySuccess(200,addressService.getById(addressId));
    }
}
